import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';

import '../../../../res/HDColors.dart';
import '../../../../view/FieldDate.dart';
import '../../../../view/FieldEditor.dart';
import '../../../../view/common_widget.dart';
import '../../../../view/image_picker_widget.dart';
import 'logic.dart';

class NetMsgPage extends StatelessWidget {
  final logic = Get.put(NetMsgLogic());
  final state = Get.find<NetMsgLogic>().state;

  @override
  Widget build(BuildContext context) {
    return GetBuilder<NetMsgLogic>(builder: (logic) {
      return SingleChildScrollView(
        child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
          ...List.generate(
              (state.gridConnectedLink.gridConnectedPointDTOS ?? []).length,
              (index) {
            return Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  FieldEditor(
                      label: ('并网点${index+1}'),

                      contentTextAlign: TextAlign.end,
                      showLine: false,

                      readOnly: true),
                  FieldEditor(
                      label: ('并网点${index+1}容量'),
                      hintText: '请输入并网点${index+1}容量',
                      contentTextAlign: TextAlign.end,
                      initialValue: state.gridConnectedLink
                          .gridConnectedPointDTOS?[index].capacity,
                      showLine: false,

                      readOnly: state.isReadOnly,
                      onChanged: (value) {
                        state.gridConnectedLink.gridConnectedPointDTOS?[index]
                            .capacity = value;
                      }),
                  FieldEditor(
                      label: '发电户号',
                      hintText: '请输入发电户号',
                      initialValue: state.gridConnectedLink
                          .gridConnectedPointDTOS?[index].electricNumber,
                      contentTextAlign: TextAlign.end,
                      showLine: false,

                      readOnly: state.isReadOnly,
                      onChanged: (value) {
                        state.gridConnectedLink.gridConnectedPointDTOS?[index]
                            .electricNumber = value;
                      }),
                  FieldEditor(
                      label: '用户编号',
                      hintText: '请输入用户编号',
                      contentTextAlign: TextAlign.end,
                      initialValue: state.gridConnectedLink
                          .gridConnectedPointDTOS?[index].correlationNumber,
                      showLine: false,

                      readOnly: state.isReadOnly,
                      onChanged: (value) {
                        state.gridConnectedLink.gridConnectedPointDTOS?[index]
                            .correlationNumber = value;
                      }),
                  FieldDate(
                      label: '购售电合同签订日期',
                      initialValue: state
                              .gridConnectedLink
                              .gridConnectedPointDTOS?[index]
                              .contractSignDate ??
                          "",
                      readOnly: state.isReadOnly,
                      minDateTime: DateFormat("yyyy-MM-dd").format(
                          DateTime.now().add(const Duration(days:-365,minutes: 1))),

                      onChanged: (value) {
                        state.gridConnectedLink.gridConnectedPointDTOS?[index]
                            .contractSignDate = value;
                      }),
                ]);
          }),
          Container(
            height: 10,
          ),
          Visibility(visible: !state.isReadOnly, child:  Align(
            alignment: Alignment.center,
            child: CommonWidget.buildBottomBtn("新增并网点", () {
              logic.add();
            }),
          ),),
          FieldEditor(
              label: '备注',
              hintText: '请输入备注信息',
              initialValue: state.gridConnectedLink.remarks ?? "",
              contentTextAlign: TextAlign.end,
              isRequired: false,
              readOnly: state.isReadOnly,
              onChanged: (value) {
                state.gridConnectedLink.remarks = value;
              }),
          Container(
            margin: const EdgeInsets.fromLTRB(13, 0, 20, 0),
            child: Text('备案表照片',
                style: TextStyle(color: HDColors.black, fontSize: 16)),
          ),
          Container(
            margin: const EdgeInsets.fromLTRB(13, 10, 20, 0),
            child: ImagePickerWidget(
                readonly: state.isReadOnly,
                controller: state.recordFormController),
          ),
          Container(
            margin: const EdgeInsets.fromLTRB(13, 10, 20, 0),
            child: Text('报装流程照片',
                style: TextStyle(color: HDColors.black, fontSize: 16)),
          ),
          Container(
            margin: const EdgeInsets.fromLTRB(13, 10, 20, 0),
            child: ImagePickerWidget(
                readonly: state.isReadOnly,
                controller: state.applicationProcessController),
          ),
          Container(
            margin: const EdgeInsets.fromLTRB(13, 10, 20, 0),
            child: Text('其他',
                style: TextStyle(color: HDColors.black, fontSize: 16)),
          ),
          Container(
            margin: const EdgeInsets.fromLTRB(13, 0, 20, 0),
            child: ImagePickerWidget(
                readonly: state.isReadOnly, controller: state.otherController),
          ),
          Visibility(visible: !state.isReadOnly, child: getFooterButton())
        ]),
      );
    });
  }

  getFooterButton() {
    return Container(
        decoration: const BoxDecoration(color: HDColors.white, boxShadow: [
          BoxShadow(
              color: HDColors.grayBg,
              offset: Offset(0, -2),
              blurRadius: 8,
              spreadRadius: 1)
        ]),
        padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 10),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: [
            CommonWidget.buildBottomBtn("暂存", () {
              logic.save();
            }),
            CommonWidget.buildBottomBtn("提交", () {
              logic.submit(1);
            }),
          ],
        ));
  }
}
